<?php
namespace app\appapi\controller;
use think\Controller;
// use think\Request;
use think\Db;
use app\appapi\WeShare;

class WePaymentresult

{
/*
     * APP支付异步通知
     * */
    public function appNotifyCheck(){
        $weapp = new WeShare(); 
        $result = $weapp->getNotifyRequestData();
        $data['return_code'] = 'FAIL';
        $data['return_msg'] = '参数格式校验错误';
        if($result){
            if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
                //$wtconfig = getWxconfig('weChatSystem');
                $wtconfig = getWxconfig('weAppSystem');//统一走小程序支付流程
                $return_sign = $result['sign'];
                unset($result['sign']);
                $sign = $weapp->getSign($result,$wtconfig['key']);
                if($return_sign == $sign){
                    try {
                        writeCretLog('微信支付'.json_encode($result));
                        $openid = $result['openid'];
                        $order_id = $result['out_trade_no'];
                        $tradeNO = $result['transaction_id'];
                        //更新订单信息状态
                        if(!empty($result['attach']) && $result['attach']){
                            writeCretLog('更新红包状态'.$result['attach']);
                            $attach = json_decode($result['attach']);
                            if($attach && is_array($attach) && $attach['id'] && $attach['type'] == 'RED_RECHARGE'){
                                $rid = $attach['id'];
                                $this->_updataRedenvelopesRecharge($openid,$order_id,1,$rid);
                            }
                        }else{
                            writeCretLog('更新红包状态'.json_encode($result));
                            $this->_updataRedenvelopesOrder($openid,$order_id,1);
                        }
                    } catch (Exception $e) {
                        writeCretLog('错误信息'.$e->getMessage());
                    } finally {
                        $data['return_code'] = 'SUCCESS';
                        $data['return_msg'] = 'OK';
                    }
                }else{
                    $data['return_code'] = 'FAIL';
                    $data['return_msg'] = '签名验证失败';
                }
                
            }
        }
        $returnXml = $weapp->arrayToXml($data);
        writeCretLog('返回结果'.$returnXml);
        echo $returnXml;
    }
    //更新红包记录状态
    private function _updataRedenvelopesOrder($openid,$order_id,$state){
        $uid = getUserOpenidInfo($openid,'User_id');
        $where['User_id'] = ['=',$uid];
        $where['Redenvelopes_id'] = ['=',$order_id];
        Db::name('redenvelopes')->where($where)->setField('State',$state);
    }
    //更新红包充值记录状态
    private function _updataRedenvelopesRecharge($openid,$order_id,$state,$rid){
        $uid = getUserOpenidInfo($openid,'User_id');
        Db::startTrans();
        try{
            $arr = getRedenvelopesRechargeInfo($order_id);
            if($arr && $arr['State'] != 1){
                if(Db::name('recharge_record')->where(array('Recharge_id'=>$order_id))->setField('State',$state)){
                    $where['User_id'] = ['=',$uid];
                    $where['Redenvelopes_id'] = ['=',$rid];
                    $amount = $arr['Amount'];
                    Db::name('redenvelopes')->where($where)->Inc('Amount',$amount)->Inc('Blance',$amount)->update(array('State'=>$state));
                }
            }
            Db::commit();
            return true;           
        }catch(\Exception $e){
            Db::rollback();
            return false;
        }
       
    }

}

